import {request} from 'umi';
import {ApplyOrderParams, QueryParams} from './data.d';
import cookie from 'react-cookies'
import {extend} from 'umi-request';

// umi-request 官方文档 https://github.com/umijs/umi-request/blob/master/README_zh-CN.md
// 通过 extend 新建一个 umi-request 实例
function createClient(baseUrl: string) {
  return extend({
    prefix: baseUrl,
    requestType: 'json',
  });
}
export async function listGroupByCategory(params?: QueryParams) {
  // 实例内部拦截器
  const client = createClient('/api');
  client.interceptors.request.use(
    (url, options) => {
      options.body = JSON.stringify(params);
      options.headers = {
        Accept: 'application/json',
        'Authorization': cookie.load('token'),
        'Content-Type': 'application/json;charset=UTF-8',
        ...options.headers,
      };
      return {
        url,
        options,
      };
    },
    {global: false}
  );
  // await client.post('/whProduct/list')
  //   .then(function (response) {
  //     return response;
  //   })
  //   .catch(function (error) {
  //     console.log(error);
  //   });
  return client.post('/whProduct/listGroupByCategory');
}

export async function categoryProductList(params?: QueryParams) {
  return request('/api/whProduct/categoryProductList', {
    requestType: 'form',
    params,
  });
}

export async function getProductInfo(params?: QueryParams) {
  return request('/api/whProduct/productInfo', {
    requestType: 'form',
    params,
  });
}

export async function applyOrder(params?: ApplyOrderParams) {
  return request('/api/whOrder', {
    method: 'POST',
    requestType: 'json',
    data: params,
  });
}
